package com.ctu.gzero.properties
{
	public class NumberOrPercentProperty extends Property
	{
		private var _maxNumberValue:Number;
		private var _minNumberValue:Number;
		private var _maxPercentValue:Number;
		private var _minPercentValue:Number;
		
		public function NumberOrPercentProperty(name:String, object:Object, min:Number, max:Number,
				minPer:String, maxPer:String)
		{
			super(name, object);
			_minNumberValue = min;
			_maxNumberValue = max;
			_minPercentValue = toNumberIfPercent(minPer);
			_maxPercentValue = toNumberIfPercent(maxPer); 
		}
		
		public function get maxNumberValue():Number
		{
			return _maxNumberValue;
		}
		
		override public function valueFromString(value:String):Object
		{
			var _digit:Number = toNumberIfPercent(value);
			
			if(!isNaN(_digit))
			{
				return value; 
			}
			
			_digit = Number(value);
			return  isNaN(_digit) ? null : _digit;
		}
		
		override public function hash(obj:Object, code:uint):uint
		{
			if(obj is String) 
			{
				return StringProperty.doHash(obj as String, code);
			}
			
			return NumberProperty.doHash(obj as Number, code);
		}
		
		public function computeActualPropertyValue(obj:Object, num:Number):Number
		{
			var _digit:Number = toNumberIfPercent(obj);
			
			if(isNaN(_digit))
			{
				return Number(obj);
			}
			
			var _pro:Number = num*(_digit/100);
			
			if(_pro <= _minNumberValue)
			{
				return _minNumberValue;
			}
			
			if(_pro >= _maxNumberValue)
			{
				return _maxNumberValue;
			}
			
			return _pro;
		}
		
		public function get maxPercentValue():Number
		{
			return _maxPercentValue;
		}
		
		public function get minNumberValue():Number
		{
			return _minNumberValue;
		}
		
		override public function setHelper(obj1:Object, obj2:Object):Object
		{
			if(obj2==null)
			{
				return obj2
			}
			
			var _digit:Number = toNumberIfPercent(obj2);
			if(!isNaN(_digit))
			{
				if(checkLowerLimit())
				{
					if(_digit <_minPercentValue)
					{
						_digit = _minPercentValue;
					}				
				}
				else 
				{	
					if(checkUpperLimit())
					{
						if(_digit>_maxPercentValue)
						{
							_digit = _maxPercentValue;
						}
					}
				}				
				return _digit.toString()+ "%";
			}
			else
			{//L2
				_digit = Number(obj2);
				if(isNaN(_digit))
				{
					return obj1;
				}
				if(checkLowerLimit())
				{
					if(_digit<_minNumberValue)
					{
						return _minNumverValue;
					}
				}
				
				if(checkUpperLimit())
				{
					if(_digit > _maxNumberValue)
					{
						return _maxNumberValue;
					}
				}
				
				return _digit;
			}			
		}
		
		public function get minPercentValue():Number
		{
			return _minPercentValue;	
		}
		
		private static function toNumberIfPercent(obj:Object):Number
		{
			if(!(obj is String))
			{
				return NaN;	
			}
			
			var _s:String = String(obj);			
			var _i:int = _s.length;
			var _tmp:Number;
			
			if(_i==0)
			{
				_tmp = (_s.charAt(_i-1)=="%") ? Number(_s.slice(0,_i-1)) : NaN;
				
			} 
			else 
			{ 
				_tmp = NaN;
			}
			
			return _tmp;
			
		} 
	}
}